<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
<script>
function write(s)
{
    document.getElementById('pre').appendChild(document.createTextNode(s));
}

var didCollect = false;
window.onmessage = function ()
{
    didCollect = true;
    gc();
}

function processStateChange(e)
{
    if (didCollect)
        write("PASS: event handler fired after garbage collection.\n");
    if (e.target.readyState == 4) {
        if (window.testRunner)
            testRunner.notifyDone();
    }
}

function test()
{
    if (window.testRunner) {
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }

    var request = new XMLHttpRequest();
    request.onreadystatechange = processStateChange;
    request.open("GET", "print-content-type.cgi", true);
    request.send(null);

    // Indirectly GC so as to ensure that 'request' isn't stack reachable.
    window.postMessage('gc!', '*');
}
</script>
</head>

<body onload="test();">
<p>This test checks whether event handlers for outstanding XMLHttpRequests survive garbage collection.</p>
<p>See https://bugs.webkit.org/show_bug.cgi?id=9113 REGRESSION (14581):
XMLHttpRequest never calls onreadystatechange with a readystate == 4</p>
<p>If the test passes, you'll see a series of 'PASS' messages below.</p>
<hr>

<pre id='pre'></pre>

</body>
</html>
